package com.future.lock.me.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.baselib.activity.BaseActivity;
import com.future.baselib.entity.MessageEvent;
import com.future.baselib.utils.EventBusUtil;
import com.future.baselib.view.CustomViewCenterDialog;
import com.future.lock.R;
import com.future.lock.common.Constants;
import com.future.lock.common.MyApp;
import com.future.lock.common.RefreshDataUtils;
import com.future.lock.common.http.HttpUtils;
import com.future.lock.common.http.response.DefaultHttpResponse;
import com.future.lock.home.entity.bean.Lock;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MachineDetailManagerActivity extends BaseActivity {

    @BindView(R.id.iv_left)
    ImageView ivLeft;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_right)
    ImageView ivRight;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.view_line)
    View viewLine;
    @BindView(R.id.layout_title)
    FrameLayout layoutTitle;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.tv_lock_id)
    TextView tvLockId;
    @BindView(R.id.tv_lock_gateway)
    TextView tvLockGateway;
    @BindView(R.id.tv_save)
    TextView tvSave;
    private CustomViewCenterDialog deleteDialog;
    private int position;

    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_machine_detail_manager);
        ButterKnife.bind(this);
    }


    @Override
    protected void initView() {
        setTitle("设备管理");
        setTitleRight("删除", v -> showDeleteDialog());
        Lock lock = Constants.lockList.get(position);
        etName.setText(lock.lock_name);
        tvLockId.setText(lock.lock_id);
        tvLockGateway.setText(lock.gateway_name);
    }

    @Override
    protected void initLogic() {

    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        position = extras.getInt("position");
    }

    @OnClick(R.id.tv_save)
    public void onViewClicked() {
        String lockName = etName.getText().toString();
        if (TextUtils.isEmpty(lockName)) {
            toast("智能锁名称不能为空");
            return;
        }
        if (TextUtils.equals(lockName, Constants.lockList.get(position).lock_name)) {
            finish();
            return;
        }
        HttpUtils.getHttpApi()
                .updateLockName(MyApp.getUserToken(), Constants.lockList.get(position).lock_id, lockName)
                .enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        DefaultHttpResponse httpResponse = new DefaultHttpResponse();
                        httpResponse.parse(response.body());
                        if (httpResponse.success == 1) {
                            EventBusUtil.sendEvent(new MessageEvent(Constants.REFRESH_LOCK_DATA));
                            Constants.lockList.get(position).lock_name = lockName;
                            setResult(0x322);
                            finish();
                        } else {
                            if (httpResponse.code == 112){
                                RefreshDataUtils.logout(mContext);
                            }
                            toast(httpResponse.msg);
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        toast(t.getMessage());
                    }
                });
    }

    private void showDeleteDialog() {
        if (deleteDialog == null) {
            View rootView = LayoutInflater.from(this).inflate(R.layout.dialog_default, null);
            ((TextView) rootView.findViewById(R.id.tv_message)).setText("确定删除该门锁吗?");
            rootView.findViewById(R.id.btn_cancel).setOnClickListener(v -> {
                if (deleteDialog != null) {
                    deleteDialog.dismiss();
                }
            });
            Button btnOk = (Button) rootView.findViewById(R.id.btn_ok);
            btnOk.setText("确定");
            btnOk.setOnClickListener(v -> {
                deleteDialog.dismiss();
                HttpUtils.getHttpApi()
                        .delLock(MyApp.getUserToken(), Constants.lockList.get(position).lock_id)
                        .enqueue(new Callback<String>() {
                            @Override
                            public void onResponse(Call<String> call, Response<String> response) {
                                DefaultHttpResponse httpResponse = new DefaultHttpResponse();
                                httpResponse.parse(response.body());
                                if (httpResponse.success == 1) {
                                    EventBusUtil.sendEvent(new MessageEvent(Constants.REFRESH_LOCK_DATA));
                                    setResult(0x321);
                                    finish();
                                } else {
                                    if (httpResponse.code == 112){
                                        RefreshDataUtils.logout(mContext);
                                    }
                                    toast(httpResponse.msg);
                                }
                            }

                            @Override
                            public void onFailure(Call<String> call, Throwable t) {
                                toast(t.getMessage());
                            }
                        });
            });
            deleteDialog = new CustomViewCenterDialog(this)
                    .setView(rootView)
                    .builder();
        }
        deleteDialog.show();
    }
}
